package com.weilive.api.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weilive.api.gate.json.protocol.AdvanceVideoModel;
import com.weilive.api.gate.json.protocol.CSA_AdvanceVideo;
import com.weilive.api.gate.json.protocol.CSA_MainView;
import com.weilive.api.gate.json.protocol.LiveModel;
import com.weilive.api.gate.json.protocol.RoomInfoModel;
import com.weilive.core.cache.redis.RedisConfig;
import com.weilive.core.cache.redis.RedisService;
import com.weilive.core.utils.Utils;

/**
 * 首页推荐
 * 
 * @author
 *
 */
@Service
public class MainViewSystem {
	private static final Logger logger = LoggerFactory.getLogger(MainViewSystem.class);
	@Autowired
	private RedisService redis;
	@Autowired
	private DataPool dataPool;

	@PostConstruct
	public void init() {
	}

	/**
	 * 显示主页信息
	 * 
	 * @return
	 */
	public CSA_MainView getMainViewMsg() {
		CSA_MainView view = new CSA_MainView();
		view.setAdList(dataPool.getAdList());
		view.setVjTypeList(dataPool.getVjTypeList());
		view.setDoyen(dataPool.getDorenList());
		// 显示列表
		List<LiveModel> showList = new ArrayList<LiveModel>();

//		LinkedHashMap<String, List<RoomInfoModel>> liveList = dataPool.getLiveShowList();
//		for (Map.Entry<String, List<RoomInfoModel>> entry : liveList.entrySet()) {
//			String[] str = entry.getKey().split(":");
//			int vjTypeId = Utils.parseInt(str[0]);
//			String vjTypeName = str[1];
//			List<RoomInfoModel> roomInfos = entry.getValue();
//			LiveModel live = new LiveModel();
//			live.setVjTypeId(vjTypeId);
//			live.setVjTypeName(vjTypeName);
//			live.setView(roomInfos);
//			// 加入
//			showList.add(live);
//		}
		// 直播列表
		List<RoomInfoModel> liveList = dataPool.getLiveList();

		LiveModel live = new LiveModel();
		live.setVjTypeId(0);
		live.setVjTypeName("推荐");
		live.setView(liveList);
		showList.add(live);
		view.setLiveList(showList);
		logger.info("获取首页推荐信息!");
		return view;
	}

	/**
	 * 显示预告信息
	 * 
	 * @return
	 */
	public CSA_AdvanceVideo getAdvanceVideoMsg(long userId) {
		CSA_AdvanceVideo view = new CSA_AdvanceVideo();
		// 固有数据
		List<AdvanceVideoModel> videoList = dataPool.getVideoList();
		// 重新构造
		List<AdvanceVideoModel> showList = new ArrayList<AdvanceVideoModel>();

		String fansKey = null;
		if (userId > 0) {
			fansKey = RedisConfig.USER_ME_FANS_PREF + userId;
		}
		for (AdvanceVideoModel v : videoList) {
			AdvanceVideoModel vdoModel = new AdvanceVideoModel();
			vdoModel.setStartedTime(v.getStartedTime());
			vdoModel.setTitle(v.getTitle());
			vdoModel.setContent(v.getContent());
			vdoModel.setPic(v.getPic());
			vdoModel.setIsFans(0);
			if (fansKey != null) {
				vdoModel.setIsFans(Utils.isFans(redis.exists(fansKey, String.valueOf(v.getUserInfo().getUserId()))));
			}
			vdoModel.setUserInfo(v.getUserInfo());
			showList.add(vdoModel);
		}

		view.setAdVideoList(showList);
		return view;
	}
}